import sys
import os 
import gcm
import logging
from django.http import HttpResponse
from sqlalchemy import MetaData, create_engine

logger = logging.basicConfig(filename='/opt/logs/rhok.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')

logger = logging.getLogger()

print 'logger', logger


config = {
'DBUSER': 'root',
'DBPASS': 'root',
'DBHOST': 'localhost',
'DBNAME': 'rhok',
}


db = create_engine('mysql://%s:%s@%s/%s?charset=utf8' % 
	(config['DBUSER'], config['DBPASS'], config['DBHOST'], config['DBNAME']),
	encoding='utf-8', echo=False, pool_recycle=30, pool_size=2, max_overflow=30, 
	echo_pool=False)


def _validate_user(register_id, phone_number):
	if not register_id:
	    logger.error('register failed - register_id "%s" invalid' % register_id)
	    return False
	if not phone_number:
	    logger.error('register failed - phone_number "%s" invalid' % phone_number)
	    return False
	
	return True

def register(request, register_id, phone_number):
	phone_number = ''
	if not _validate_user(register_id, phone_number):
	    return HttpResponse('register user failed, register_id or phone_number are invalid')       
	
	db_conn = db.connect()
	query = 'insert into user (register_id, phone_number) values (%s, %s)' % (register_id, phone_number)
	data = db_conn.execute(query)
	return HttpResponse("OK")

def share_location(request):
	return HttpResponse("OK")

def get_location(request):
	return HttpResponse("OK")

